let pageSize = $("#pageSize").val();
let currentPage = $("#currentPage").text();

$("#addStudentsBtn").click(function () {
    let name = $("#addStudentsName").val();
    let age = $("#addStudentsAge").val();
    let sex = $("[name='addStudentsGender']:checked").val();
    let subjectsId = $("#selectMajor").val();
    let classesId = $("#selectClasses").val();
    console.log(subjectsId, classesId);
    // console.log(name,age,sex);
    $.ajax({
        url: '/students/createStudents',
        type: 'POST',
        data: {
            name, age, sex, subjectsId, classesId
        },
        success: function (req, res) {
            // console.log('新增结果', res);
            alert('新增成功')
            studentsInit();
        }
    })
})

async function studentsInit({ pageSize, currentPage } = {}) {
    let StudentsData = await getStudentsData({ pageSize, currentPage } );
    console.log(StudentsData);
    studentsRender(StudentsData);
}
studentsInit({ pageSize, currentPage } = {});
function getStudentsData({ pageSize, currentPage } = {}) {
    return new Promise((resolve, reject) => {
        $.ajax({
            url: './students/getStudents',
            type: 'get',
            data: {
                pageSize: 3, //每页显示条数
                currentPage: 1 //当前页数
            },
            success(res) {
                // console.log('等待学生数据',res);
                // console.log(res);
                if (res.code) {
                    console.log(res);
                    resolve(res.result);
                }
            }

        })
    })
}

function studentsRender(data) {
    // console.log(data);
    let studentsHtml = data.map(item => {
        console.log(item);
        return `
    <tr>
        <td>${item._id}</td>
        <td>${item.name}</td>
        <td>${item.age}</td>
        <td>${item.sex}</td>
        <td class="studentsSubject">${item.subjectsId.name}</td>
        <td>${item.classesId.name}</td>
        <td>学生头像</td>
        <td>班主任</td>
        <td>
        <a href="#/studentsUpdate?id=${item._id}" class="btn1" data-id="${item._id}">修改</a>
        <a class='btn2' data-id="${item._id}">删除</a>
        </td>
    </tr>
        `
    }).join('')
    $("#studentsTb").html(studentsHtml);

    $("#updateName").val(data[0] ? data[0].name : '暂无');
    $("#updateAge").val(data[0] ? data[0].age : '暂无');
    // $(`[name='addStudentsGender'][value='${data[0].value?data[0].value:'暂无'}']`).attr("checked",true); 
}

$("body").on('click', '.btn2', (function () {
    const _id = $(this).attr('data-id');

    return new Promise((resolve, reject) => {
        $.ajax({
            url: '/students/deleteStudents',
            type: 'POST',
            data: {
                _id
            },
            success(res) {
                console.log('删除结果', res);
                if (res.code) {


                    studentsInit();
                }


            }

        })
    })



}))


$("body").on('click', '.btn1', (function () {
    const _id = $(this).attr('data-id');


    return new Promise((resolve, reject) => {
        $.ajax({
            url: '/students/updateStudents',
            type: 'POST',
            data: {
                _id,
            },
            success(res) {
                console.log('修改结果', res);
                if (res.code) {
                    console.log(res);
                    $("#updateName").val(res.result[0].name);
                    $("#updateAge").val(res.result[0].age);
                    $(`[name='addStudentsGender'][value='${res.result[0].sex}']`).attr("checked", true);
                    $("#subjectId").val(res.result[0].subjectsId)
                    $("#classesId").val(res.result[0].classesId)
                    // getClassesBySubjectsId(res.result[0].classesId)
                    studentsInit();
                }
            }
        })
    })
}))


$("#selectMajor").change(function () {
    const subjectId = $(this).val();
    console.log(subjectId);
    getClassesBySubjectsId(subjectId);

})

$("#subjectId").change(function () {
    const subjectId = $(this).val();
    console.log(subjectId);
    getClassesBySubjectsId(subjectId);

})
